//
//  CurrentEventDetailController.m
//  Runone
//
//  Created by runone02 on 16/8/28.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "CurrentEventDetailController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "Define.h"
#import "IncidentDetailCell.h"
#import "ROPointAnnotation.h"
#import "CustomAnnotationView.h"
#import "CustomCallOutView.h"
#import "RequestGinsengModel.h"
#import "RequestHelper.h"
#import "AppLocalManager.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "EventDeatilModel.h"
#import "EventTopView.h"
@interface CurrentEventDetailController ()<MAMapViewDelegate>

@property (strong, nonatomic) UITableView *tableView;

@property (nonatomic, strong) MAMapView *mapView;

@property (nonatomic, strong)NSMutableArray *dataArray;

@property (nonatomic, strong) EventTopView *EventTopView;

@end

@implementation CurrentEventDetailController

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //判断传入的模型是什么类型的
    self.title = [self eventType];
    
    [self loadMapView];
    [self configMap];
    [self loadAnnotaion];

    [self configTopView];
   // Construction
    if ([self.title isEqualToString:@"施工事件"]) {
        [self loadConstructionData];
    }else{
    [self loadData];
    }
    [self showCameraGunArround2KM];
    
}

- (NSString *)eventType {
    NSString *string = nil;
    if ((self.CEModel.IncidentType == 1)) {
        string = [NSString stringWithFormat:@"事故事件"];
    }else { 
        string = [NSString stringWithFormat:@"施工事件"];
    }
    return string;
}

-(void)loadMapView
{
    [AMapServices sharedServices].apiKey = GETGAODEPAIKEY;
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,200)];
    _mapView.delegate = self;
    _mapView.showsCompass = NO;
    _mapView.showsScale= NO;
    [self.view addSubview:_mapView];
}

- (void)loadAnnotaion {
    NSMutableArray *pointArray = [[NSMutableArray alloc] init];
    ROPointAnnotation *pointAnnotation = [[ROPointAnnotation alloc] init];
    float latitude = [self.CEModel.Latitude floatValue];
    float longitude = [self.CEModel.Longitude floatValue];
    pointAnnotation.coordinate=CLLocationCoordinate2DMake(latitude,longitude);
    pointAnnotation.currentEventModel = self.CEModel;
    [pointArray addObject:pointAnnotation];
    [_mapView addAnnotation:pointAnnotation];
    [_mapView showAnnotations:pointArray animated:YES];
}

- (void)configMap {
    MACoordinateSpan span = MACoordinateSpanMake(0.08, 0.08);
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(25.1902561, 113.212982);
    MACoordinateRegion region = MACoordinateRegionMake(centerCoordinate, span);
    _mapView.region = region;
}

- (void)configTopView {
    self.EventTopView =[EventTopView eventTopView];
    self.EventTopView.frame = CGRectMake(0, 210, self.view.bounds.size.width, 250);
    [self.view addSubview:self.EventTopView];
}
#pragma mark - 请求
//下拉 GetAccidentEventDetailInfo
- (void)loadData {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetAccidentEventDetailInfoModel=[[RequestGinsengModel alloc] init];
    GetAccidentEventDetailInfoModel.URL         = GetEventDataURL;
    GetAccidentEventDetailInfoModel.DataValue   = [self DataValueStr:self.CEModel.IncidentUID];
    GetAccidentEventDetailInfoModel.UpdatedTime = @"";
    GetAccidentEventDetailInfoModel.SystemCode  = @"GuangleHighway";
    GetAccidentEventDetailInfoModel.MethodName  = @"GetAccidentEventDetailInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        EventDeatilModel *model = dataValueModel;
        self.EventTopView.detailModel = model;
    };
    [requestHelper RequestGinsengModel:GetAccidentEventDetailInfoModel];
}

- (void)loadConstructionData {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetAccidentEventDetailInfoModel=[[RequestGinsengModel alloc] init];
    GetAccidentEventDetailInfoModel.URL         = GetEventDataURL;
    GetAccidentEventDetailInfoModel.DataValue   = [self DataValueStr:self.CEModel.IncidentUID];
    GetAccidentEventDetailInfoModel.UpdatedTime = @"";
    GetAccidentEventDetailInfoModel.SystemCode  = @"GuangleHighway";
    GetAccidentEventDetailInfoModel.MethodName  = @"GetConstructionEventDetailInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        EventDeatilModel *model = dataValueModel;
        self.EventTopView.detailModel = model;
    };
    [requestHelper RequestGinsengModel:GetAccidentEventDetailInfoModel];
}


- (void)showCameraGunArround2KM {
    
    AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate ;
    NSArray *dataSourceArray=myDelegate.AllDeviceBaseArray ;
    NSMutableArray *cameraPointsArray = [[NSMutableArray alloc] init];//存放在区域内的摄像枪点
    CGFloat latitude = [self.CEModel.Latitude floatValue];
    CGFloat longtitude = [self.CEModel.Longitude floatValue];
    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(latitude, longtitude);
    MAMapPoint vehiclePoint = MAMapPointForCoordinate(coordinate2D);//事件在地图上的所在点
    for (DeviceBaseInfo *baseInfoModel in dataSourceArray) {
        CLLocationCoordinate2D coordinate =CLLocationCoordinate2DMake(baseInfoModel.Latitude, baseInfoModel.Longitude);
        MAMapPoint point = MAMapPointForCoordinate(coordinate);
        CLLocationDistance distance = MAMetersBetweenMapPoints(point,vehiclePoint);
        if (distance <= 2000) {
            ROPointAnnotation *pointAnnotation = [[ROPointAnnotation alloc] init];
            CGFloat latitude  = baseInfoModel.Latitude;
            CGFloat longitude = baseInfoModel.Longitude;
            pointAnnotation.deviceModel = baseInfoModel;
            pointAnnotation.coordinate=CLLocationCoordinate2DMake(latitude,longitude);
            [cameraPointsArray addObject:pointAnnotation];//添加到数组中
        }
    }
    [_mapView addAnnotations:cameraPointsArray];
    [_mapView showAnnotations:cameraPointsArray animated:YES];
}

#pragma mark - tableviewDelegate


#pragma mark -地图代理
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= NO;       //设置气泡可以弹出，默认为NO
        
        ROPointAnnotation *anno = (ROPointAnnotation *)annotation;
        annotationView.CEModel = anno.currentEventModel;
        annotationView.deviceModel = anno.deviceModel;
        if (annotationView.deviceModel ) {
            annotationView.image = [UIImage imageNamed:@"摄像枪图标"];
        }else if (annotationView.CEModel.IncidentType == 1 ) {
         annotationView.image = [UIImage imageNamed:@"事故图标"];//事故图标
        }else {
         annotationView.image = [UIImage imageNamed:@"主线施工图标"];// 施工图标
        }
        return annotationView;
    }
    return nil;
}








-(NSString *)DataValueStr:(NSString *)IncidentUID
{
    NSString *valueStr =[NSString stringWithFormat:@"[{\"key\":\"EventUID\",\"value\":\"%@\"}]",IncidentUID];
    return valueStr;
}



@end
